<template>
  <div class="app-container">
    <el-table v-if="total>0" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          <span>{{ scope.$index+(listQuery.page-1)*10+1 }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" label="标题">
        <template slot-scope="{row}">
          <router-link :to="'/operation/look/'+row.articleId" class="link-type">
            <span>{{ row.blogTitle.substring(0,50) }}...</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column v-if="!isUserInfo" align="center" label="收藏" width="120">
        <template slot-scope="scope">
          <span>
            <el-button class="el-button" type="primary" size="small" icon="el-icon-star-off" @click.native="cancelCollect(scope.row.articleId)">取消收藏</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <el-table v-else />
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>
<!--
0:
articleId: 61
blogCollections: 1
blogTime: "2021-07-18 07:44:20"
blogTitle: "俄语389陃"
blogType: 1
blogViews: 588
id: 79
lastTime: "2022-13-15 13:36:11"
userId: "1470030077541515265"
-->

<script>
import { getCollection, addOrDeleteCollection } from '@/api/attach'
import Pagination from '@/components/Pagination'
export default {
  name: 'Collectionslist',
  components: { Pagination },
  props: {
    isUserInfo: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      articleId: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    const userId = this.$route.params && this.$route.params.id
    if (userId !== undefined) { this.getList(userId) } else {
      const id = this.$store.state.user.id
      this.getList(id)
    }
  },
  methods: {
    getList(id) {
      this.listLoading = true
      getCollection(id, this.listQuery.page, this.listQuery.limit)
        .then(res => {
          const { blog, total } = res.data
          this.total = total
          if (this.total > 0) {
            this.list = blog
          }
          this.listLoading = false
        })
    },
    cancelCollect(articleId) {
      console.log(articleId)
      addOrDeleteCollection(this.$store.state.user.id, articleId, false)
        .then(res => {
          this.$message({
            message: '已取消收藏',
            type: 'success'
          })
          this.getList(this.$store.state.user.id)
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '取消收藏失败',
            type: 'error'
          })
        })
    }
  }
}
</script>

<style scoped>
</style>
